<?php
namespace app\system\controller;

use app\system\model\GroupLinkModel;
use app\system\model\GroupModel;
use app\system\model\LinkModel;

class Group extends Common
{
    protected $pageSize = 20;

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {

        $groupModel = new GroupModel();
        $list = $groupModel->selectGroup();

        $assign = [
            'list' => $list,
        ];
        return $this->fetch('', $assign);
    }

    /**
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if (isAjaxPost()) {
            $data = input('post.');

            if (strlen($data['title']) == 0) {
                return $this->jsonError('请输入用户组名称');
            }

            if (empty($data['link']) || !is_array($data['link'])) {
                return $this->jsonError('请输入选择操作权限');
            }

            $groupModel = new GroupModel();
            if ($gid = $groupModel->addGroup($data)) {
                $groupLinkModel = new GroupLinkModel();
                $datas = [];
                foreach ($data['link'] as $v) {
                    $datas[] = [
                        'group_id' => $gid,
                        'link_id' => $v['id']
                    ];
                }
                $groupLinkModel->addGroupLink($datas);

                return $this->jsonSuccess('添加成功');
            } else {
                return $this->jsonError('添加失败');
            }
        }

        $linkModel = new LinkModel();
        $list = $linkModel->selectLink([], false, 'sort asc,id desc');

        $assign = [
            'linkList' => classnav(classsorting(obj2array($list))),
        ];
        return $this->fetch('', $assign);
    }

    /**
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save($id)
    {
        $id = intval($id);
        if ($id <= 0) {
            $this->redirect('index');
        }

        $groupModel = new GroupModel();
        $groupLinkModel = new GroupLinkModel();

        if (isAjaxPost()) {
            $data = input('post.');

            if (strlen($data['title']) == 0) {
                return $this->jsonError('请输入用户组名称');
            }

            if (empty($data['link']) || !is_array($data['link'])) {
                return $this->jsonError('请输入选择操作权限');
            }

            if ($groupModel->updateGroup(['title' => $data['title']], ['id' => $data['id']]) !== false) {
                $groupLinkModel->deleteGroupLink(['group_id' => $data['id']]);
                $datas = [];
                foreach ($data['link'] as $v) {
                    $datas[] = [
                        'group_id' => $data['id'],
                        'link_id' => $v['id']
                    ];
                }
                $groupLinkModel->addGroupLink($datas);

                return $this->jsonSuccess('修改成功');
            } else {
                return $this->jsonError('修改失败');
            }
        }

        $linkModel = new LinkModel();
        $list = $linkModel->selectLink([], false, 'sort asc,id desc');
        $info = $groupModel->findGroup($id);

        //当前组的权限列表
        $column = $groupLinkModel->columnGroupLink('link_id', ['group_id'=>$id]);
        $list = obj2array($list);
        foreach ($list as &$v) {
            if (in_array($v['id'], $column)) {
                $v['checked'] = true;
            } else {
                $v['checked'] = false;
            }
        }
        $list = classnav(classsorting($list));

        $assign = [
            'linkList' => $list,
            'data' => $info,
        ];
        return $this->fetch('', $assign);
    }

    /**
     * @param int $id
     * @return \think\response\Json
     */
    public function del($id) {
        !isAjaxPost() && die;
        $id = intval($id);
        if ($id <= 0) {
            return $this->jsonError('删除失败');
        }

        $groupModel = new GroupModel();
        $groupLinkModel = new GroupLinkModel();
        if ($groupModel->deleteGroup($id)) {
            $groupLinkModel->deleteGroupLink(['group_id' => $id]);
            return $this->jsonSuccess('删除成功');
        } else {
            return $this->jsonError('删除失败');
        }
    }
}